#!/bin/bash

# 仅适用与 CDH 集群的solr安装

#
CDH_zkcli_SHELL=$HADOOP_CLUSTER_HOME/lib/solr/bin/zkcli.sh
# solr和HanLP的webapp程序的部署目录
HanLP_HADOOP_WEBAPP_DIR=$HADOOP_CLUSTER_HOME/lib/solr/webapps/HanLP
Solr_HADOOP_WEBAPP_DIR=$HADOOP_CLUSTER_HOME/lib/solr/webapps/solr
Solr_HADOOP_WEBAPP_CLASSES_DIR=$Solr_HADOOP_WEBAPP_DIR/WEB-INF/classes

#solr的配置
conf_solr(){
  local value_arr=($HRS_SOLR_HOSTS)
  for i in ${value_arr[@]}; do
    echo_info "deal on [$i] :"
    
    echo_doing "init dir ... ..."
    # 每台主机创建一个HRS_SOLR_HOME目录，但是目前暂时没有用处
    ssh -o StrictHostKeyChecking=no $i "if [ -d $HRS_SOLR_HOME ]; then rm -rf $HRS_SOLR_HOME/*; else mkdir -p $HRS_SOLR_HOME; fi" || { die "init $HRS_SOLR_HOME fail"; }
    ssh -o StrictHostKeyChecking=no $i "if [ -d $HRS_SOLR_LOGS ]; then rm -rf $HRS_SOLR_LOGS/*; else mkdir -p $HRS_SOLR_LOGS; fi" || { die "init $HRS_SOLR_LOGS fail"; }
    # 建立 HanLP_HADOOP_WEBAPP_DIR 目录
    ssh -o StrictHostKeyChecking=no $i "if [ -d $HanLP_HADOOP_WEBAPP_DIR ]; then rm -rf $HanLP_HADOOP_WEBAPP_DIR/*; else mkdir -p $HanLP_HADOOP_WEBAPP_DIR; fi" || { die "make dict dir fail"; }
    # 建立 Solr_HADOOP_WEBAPP_CLASSES_DIR 目录
    ssh -o StrictHostKeyChecking=no $i "if [ ! -d $Solr_HADOOP_WEBAPP_CLASSES_DIR ]; then mkdir -p $Solr_HADOOP_WEBAPP_CLASSES_DIR; fi" || { die "make solr classes dir fail"; }
    echo_done

    echo_doing "deliver dict and properties ... ..."
    # 发布文件给 HanLP_HADOOP_WEBAPP_DIR 和 Solr_HADOOP_WEBAPP_CLASSES_DIR
    scp -r $XDIR/packages/solr_CDH/HanLP/* $i:$HanLP_HADOOP_WEBAPP_DIR >> $SETUP_LOGFILE || { die "scp fail : from [$XDIR/packages/solr_CDH/HanLP/*] TO [$HanLP_HADOOP_WEBAPP_DIR]"; }
    scp -r $XDIR/packages/solr_CDH/classes/* $i:$Solr_HADOOP_WEBAPP_CLASSES_DIR >> $SETUP_LOGFILE || { die "scp fail : from [$XDIR/packages/solr_CDH/classes/*] TO [$Solr_HADOOP_WEBAPP_CLASSES_DIR]"; }
    echo_done
    
    echo_doing "conf properties ... ..."
    # 修改 Solr_HADOOP_WEBAPP_CLASSES_DIR 各属性文件
    ssh -o StrictHostKeyChecking=no $i "sed -i \"s%^log4j\.appender\.file\.File.*%log4j\.appender\.DailyFile\.File=$HRS_SOLR_LOGS\/hrssolr.log%\" $Solr_HADOOP_WEBAPP_CLASSES_DIR/log4j.properties" || { echo_error "solr classes log4j.properties sed DailyFile fail"; }
    ssh -o StrictHostKeyChecking=no $i "sed -i \"s/dbUrl=jdbc:postgresql.*/dbUrl=jdbc:postgresql:\/\/$HRS_PGSQL_HOST:$HRS_PGSQL_PORT\/$HRS_PGSQL_DBNAME/\" $Solr_HADOOP_WEBAPP_CLASSES_DIR/database-plugin.properties" || { echo_error "solr classes pgsql sed dbUrl fail"; }
    ssh -o StrictHostKeyChecking=no $i "sed -i \"s/^dbUser=.*/dbUser=$HRS_PGSQL_DBUSER/\" $Solr_HADOOP_WEBAPP_CLASSES_DIR/database-plugin.properties" || { echo_error "solr classes pgsql sed dbUser fail"; }
    ssh -o StrictHostKeyChecking=no $i "sed -i \"s/^dbPasswd=.*/dbPasswd=$HRS_PGSQL_DBPAWD/\" $Solr_HADOOP_WEBAPP_CLASSES_DIR/database-plugin.properties" || { echo_error "solr classes pgsql sed dbPasswd fail"; }
    ssh -o StrictHostKeyChecking=no $i "sed -i \"s/CDH-5.7.4-1.cdh5.7.4.p0.2/CDH/\" $Solr_HADOOP_WEBAPP_CLASSES_DIR/hanlp.properties" || { die "solr classes dict properties sed fail"; }
    ssh -o StrictHostKeyChecking=no $i "if [ ! -d \"\`grep '^[[:space:]]*root=.*' ${Solr_HADOOP_WEBAPP_CLASSES_DIR}/hanlp.properties | sed -e 's/.*=//'\`\" ]; then exit 1; fi" || { die "solr hanlp.properties [root] conf is not dir"; }
    echo_done
   
    echo_doing "send jars ... ..."
    # 部署依赖jar包
    scp $XDIR/packages/solr_CDH/HanLP_JAR/hanlp-1.3.0.jar $i:$Solr_HADOOP_WEBAPP_DIR/WEB-INF/lib >> $SETUP_LOGFILE || { die "scp fail : from [$XDIR/packages/solr_CDH/HanLP_JAR/hanlp-1.3.0.jar] TO [$Solr_HADOOP_WEBAPP_DIR/WEB-INF/lib]"; }

    scp $XDIR/packages/solr_CDH/HanLP_JAR/Hanlp_4.x/hanlp-solr4.10.3-plugin-1.2.0.jar $i:$Solr_HADOOP_WEBAPP_DIR/WEB-INF/lib >> $SETUP_LOGFILE || { die "scp fail : from [$XDIR/packages/solr_CDH/HanLP_JAR/Hanlp_4.x/hanlp-solr4.10.3-plugin-1.2.0.jar] TO [$Solr_HADOOP_WEBAPP_DIR/WEB-INF/lib]"; }
   
    scp $XDIR/packages/solr_CDH/postgresql-9.4.1212.jre7.jar $i:$Solr_HADOOP_WEBAPP_DIR/WEB-INF/lib >> $SETUP_LOGFILE || { die "scp fail : from [$XDIR/packages/solr_CDH/postgresql-9.4.1212.jre7.jar] TO [$Solr_HADOOP_WEBAPP_DIR/WEB-INF/lib]"; }

    echo_done
  done
}

setup_solr() {
  echo_start "$1 ... ..."
  confirm_operate "install solr"
  if [ $? -eq 0 ]; then
    if [ "x$HRS_SOLRSTART_HOST" = "x" ]; then
      echo_warn "The variable HRS_SOLRSTART_HOST does not assign a value, Unable to install! "
      echo_passed "$1 SKIP"
      return 0
    fi
    # 检查主服务主机配置是否正确
    local is_HRS_SOLRSTART_HOST_ok="false"
    local value_arr=($HRS_SOLR_HOSTS)
    for i in ${value_arr[@]}; do
      if [ "x$HRS_SOLRSTART_HOST" = "x$i" ]; then
        is_HRS_SOLRSTART_HOST_ok="true"
        break
      fi
    done
    if [ "$is_HRS_SOLRSTART_HOST_ok" = "false" ]; then die "HRS_SOLRSTART_HOST[$HRS_SOLRSTART_HOST] is not in [$HRS_SOLR_HOSTS]  Setup Abort! "; fi

    # 配置solr
    conf_solr

    # 组装zk主机列表
    if [ "x$HRS_SOLR_ZK_ENSEMBLE" = "x" ]; then
      die "HRS_SOLR_ZK_ENSEMBLE=$HRS_SOLR_ZK_ENSEMBLE is WRONG! "
    fi
    # 建立solr主服务主机上的配置信息
    echo_info "make solrstart host on [$HRS_SOLRSTART_HOST] ... ..."
    scp $XDIR/packages/solr_CDH/classes/log4j.properties $HRS_SOLRSTART_HOST:$HADOOP_CLUSTER_HOME/lib/solr/bin >> $SETUP_LOGFILE || { die "scp to [$HRS_SOLRSTART_HOST] fail : from [$XDIR/packages/solr_CDH/classes/log4j.properties] TO [$HADOOP_CLUSTER_HOME/lib/solr/bin]"; }

    scp $XDIR/packages/solr_CDH/classes/log4j.properties $HRS_SOLRSTART_HOST:$HADOOP_CLUSTER_HOME/lib/solr/bin >> $SETUP_LOGFILE || { die "scp to [$HRS_SOLRSTART_HOST] fail : from [$XDIR/packages/solr_CDH/classes/log4j.properties] TO [$HADOOP_CLUSTER_HOME/lib/solr/bin]"; }

    ssh -o StrictHostKeyChecking=no $HRS_SOLRSTART_HOST "if [ -d $HYRENTMP_HOME/solr_CDH ]; then rm -rf $HYRENTMP_HOME/solr_CDH; fi" || { die "del $HYRENTMP_HOME/solr_CDH fail"; }
    scp -r $XDIR/packages/solr_CDH $HRS_SOLRSTART_HOST:$HYRENTMP_HOME >> $SETUP_LOGFILE || { die "scp to [$HRS_SOLRSTART_HOST] fail : from [$XDIR/packages/solr_CDH] TO [$HYRENTMP_HOME]"; }

    ssh -o StrictHostKeyChecking=no $HRS_SOLRSTART_HOST "$CDH_zkcli_SHELL -zkhost $HRS_SOLR_ZK_ENSEMBLE -cmd upconfig -confdir $HYRENTMP_HOME/solr_CDH/fullTextIndexing_conf -confname HrdsFullTextIndexing" || { die "Upload fullTextIndexing conf file to fail"; }
    #安装solr on hbase
    ssh -o StrictHostKeyChecking=no $HRS_SOLRSTART_HOST "$CDH_zkcli_SHELL -zkhost $HRS_SOLR_ZK_ENSEMBLE -cmd upconfig -confdir $HYRENTMP_HOME/solr_CDH/solrOverHbase_conf -confname HrdsHbaseOverSolr" || { die "Upload solr_habse_file to fail"; }
    echo_info "make solrstart host on [$HRS_SOLRSTART_HOST] ... ... DONE"

    # 设置环境变量
    echo_info "set profile on all solr hosts ... ..."
    local value_arr=($HRS_SOLR_HOSTS)
    for i in ${value_arr[@]}; do
      echo_doing "  host : $i >>>>>> "
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_SOLR_HOME" "$HRS_SOLR_HOME" "$HYREN_USER"
      ssh -o StrictHostKeyChecking=no $i 'bash -s' < $XDIR/util/func_rmot_setprofile.sh "HRS_SOLR_LOGS" "$HRS_SOLR_LOGS" "$HYREN_USER"
      echo_done
    done

    echo_success "$1"
    echo_info "****************************************"
    echo_info "SOLR HOME   dir  : $HRS_SOLR_HOME"
    echo_info "SOLR LOGS   dir  : $HRS_SOLR_LOGS"
    echo_info "****************************************"

    echo_warn "========================================"
    echo_warn " 请进入CDH管理界面，重启solr！"
    echo_warn " 创建core、shard、collection的URL："
    echo_warn " http://$HRS_SOLRSTART_HOST:8983/solr/admin/collections?action=CREATE&name=HrdsFullTextIndexing&numShards=2&replicationFactor=2&maxShardsPerNode=3"
    echo_warn " > $HRS_SOLRSTART_HOST:8983/solr   ：Solr的访问地址，可以是任意一台solr主机的IP"
    echo_warn " > HrdsFullTextIndexing            ：Core名称"
    echo_warn " > numShards                       ：分片数"
    echo_warn " > replicationFactor               ：副本集数量"
    echo_warn " > maxShardsPerNode                ：每个Solr服务器节点上最大Shard数量"
    echo_warn "========================================="

    echo_passed "$1 done."
  else
    echo_passed "$1 SKIP"
  fi
}

